﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using BCEntity.OA.Attendance.AttendanceWiFi;
using BCCommon;

namespace BCData.OA.Attendance.AttendanceWiFi
{
    public class AttendanceWiFiData : IAttendanceWiFiData
    {
        private const string QUERY = @"SELECT 
	                                    Id,
	                                    GroupId,
	                                    Name,
	                                    MAC,
	                                    ModeId,
	                                    AdminId,
	                                    AdminName,
	                                    CreateTime,ModifierId,
	                                    ModifierName,
	                                    ModifyTime,
	                                    Status
                                    FROM OA_AttendanceWiFi  
                                    WHERE Status<>'Abandon'";

        private readonly IDatabaseContext databaseContext;

        public AttendanceWiFiData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        /// <summary>
        /// 校验WiFi信息存在不
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public async Task<bool> CheckIsExist(long id)
        {
            string sql = @"SELECT 1 FROM OA_AttendanceWiFi WHERE Id=?Id AND Status<>'Abandon' LIMIT 1";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?Id", id, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameter) != null;
        }

        /// <summary>
        /// 删除WiFi
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="adminName">管理员信息</param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(long id, string adminName = null)
        {
            string sql = SqlCommands.GetUpdateSql("OA_AttendanceWiFi", new string[] { "Status", "ModifierName", "ModifyTime" }, new string[] { "Id" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?Id", id, DbType.Int64);
            parameters.Add("?Status", "Abandon", DbType.AnsiString, size: 50);
            parameters.Add("?ModifierName", adminName, DbType.AnsiString, size: 50);
            parameters.Add("?ModifyTime", DateTime.Now, DbType.DateTime);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction);
        }

        /// <summary>
        /// 获取考勤WiFi信息列表
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<AttendanceWiFiEntity>> GetAsync()
        {
            return await this.databaseContext.Slave.QueryAsync<AttendanceWiFiEntity>(string.Format("{0}", QUERY));
        }

        /// <summary>
        /// 获取WiFi信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public async Task<AttendanceWiFiEntity> GetEntityAsync(long id)
        {
            string sql = string.Format("{0} AND  Id=?Id;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?Id", id, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<AttendanceWiFiEntity>(sql, parameters);
        }

        /// <summary>
        /// 新创建一个考勤WiFi信息
        /// </summary>
        /// <param name="entity">WiFi信息</param>
        /// <returns></returns>
        public async Task<AttendanceWiFiEntity> InsertAsync(AttendanceWiFiEntity entity)
        {
            DynamicParameters parameters = new DynamicParameters();
            string sql = SqlCommands.GetInsertSql("OA_AttendanceWiFi", new string[] { "ModeId", "GroupId", "Name", "MAC", "AdminId", "AdminName" });
            parameters.Add("?ModeId", entity.ModeId, DbType.Int64);
            parameters.Add("?GroupId", entity.GroupId, DbType.Int64);
            parameters.Add("?Name", entity.Name, DbType.AnsiString, size: 50);
            parameters.Add("?MAC", entity.MAC, DbType.AnsiString, size: 50);
            parameters.Add("?AdminId", entity.AdminId, DbType.Int64);
            parameters.Add("?AdminName", entity.AdminName, DbType.AnsiString, size: 30);

            string sqlQuery = string.Format("{0}{1} AND Id=LAST_INSERT_ID();", sql, QUERY);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<AttendanceWiFiEntity>(sqlQuery, parameters, databaseContext.Transaction);
        }

        /// <summary>
        /// 修改WiFi信息
        /// </summary>
        /// <param name="entity">WiFi信息</param>
        /// <returns></returns>
        public async Task<AttendanceWiFiEntity> UpdateAsync(AttendanceWiFiEntity entity)
        {
            DynamicParameters parameters = new DynamicParameters();
            string sql = SqlCommands.GetUpdateSql("OA_AttendanceWiFi", new string[] { "ModeId", "Name", "MAC", "ModifierName", "ModifyTime", "ModifierId" }, new[] { "Id", });
            parameters.Add("?Id", entity.Id, DbType.Int64);
            parameters.Add("?Name", entity.Name, DbType.AnsiString, size: 50);
            parameters.Add("?MAC", entity.MAC, DbType.AnsiString, size: 500);
            parameters.Add("?ModifierId", entity.ModifierId, DbType.Int64);
            parameters.Add("?ModifierName", entity.ModifierName, DbType.AnsiString, size: 30);
            parameters.Add("?ModifyTime", DateTime.Now, DbType.DateTime);

            string sqlQuery = string.Format("{0}{1} AND Id=?Id;", sql, QUERY);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<AttendanceWiFiEntity>(sqlQuery, parameters, databaseContext.Transaction);
        }

        public async Task<bool> CheckIsExist(string mac)
        {
            string sql = @"SELECT 1 FROM OA_AttendanceWiFi WHERE  UPPER(MAC)=?MAC  AND Status<>'Abandon' LIMIT 1";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?MAC", mac.ToUpper(), DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameter) != null;
        }

        public async Task<IEnumerable<AttendanceWiFiEntity>> GetListByModeIdAndGroupId(long groupId)
        {
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GroupId", groupId, DbType.Int64);
            return await this.databaseContext.Slave.QueryAsync<AttendanceWiFiEntity>(string.Format("{0} AND  GroupId=?GroupId", QUERY), parameters);
        }
    }
}
